package com.threader.bo;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Random;

import com.threader.ui.PeoplesTableModel;

public class Place extends Observable {

	public class Post {
		private PeoplesTableModel peoplesTableModel = new PeoplesTableModel();
		private int salary;
		private Place worckPlace;

		public Post(Place wp){
			setWorckPlace(wp);
			setSalary((new Random().nextInt(9)+1)*100);
		}
		
		public boolean addPeople(People people, int salary){
			peoplesTableModel.addPeople(people);
			people.setPost(this);
			setSalary(salary);
			stateChanged();
			return true;
		}

		public boolean delPeople(People people){
			for(int i=0; i<peoplesTableModel.getRowCount();i++){
				if(peoplesTableModel.getPeople(i)==people){
					peoplesTableModel.delPeople(i);
					stateChanged();
					return true;
				}
			}
			stateChanged();
			return false;
		}

		public void delAllPeople(){
			for(int i=0; i<peoplesTableModel.getRowCount();i++)
				peoplesTableModel.delPeople(i);
			stateChanged();
		}
		
		public int getPeoplesCount(){
			return peoplesTableModel.getRowCount();
		}

		public People getPeople(int row){
			return peoplesTableModel.getPeople(row);
		}

		public void setWorckPlace(Place worckPlace) {
			this.worckPlace = worckPlace;
		}

		public Place getWorckPlace() {
			return worckPlace;
		}

		public void setSalary(int salary) {
			this.salary = salary;
		}

		public int getSalary() {
			return salary;
		}
	}
	
	private ArrayList<Post> PostsList = new ArrayList<Post>();
	
	private long id;

	private String Name;

	private Time time;
	
	private double executed;

//	private double worckCount;

//	String name, Time time, int postsCount
	public Place() {
//		setTime(time);
//		setName(name);
		setPostsCount(5);
	}
	
	public Place setTime(Time time) {
		this.time = time;
		return this;
	}
	public Time getTime(){
		return time;
	}
	private void datingProcess(People people){
		for(Post p: PostsList){
			for(int i = 0; i < p.getPeoplesCount(); i++){
				people.getMemory().rememberPeople(p.getPeople(i));
			}
		}
	}
	
	public void addPeople(Post post, People people, int salary){
		datingProcess(people);
		post.addPeople(people, salary);
		stateChanged();
	}

	public boolean delPeople(People people){
		for(int i = 0; i<PostsList.size(); i++){
			if(PostsList.get(i).delPeople(people))
				return true;
		}
		stateChanged();
		return false;
	}

	public void delAllPeople(){
		for(int i = 0; i<PostsList.size(); i++)
			PostsList.get(i).delAllPeople();
		stateChanged();
	}

	public double getPayment(People p){
		for(int i = 0; i<PostsList.size(); i++){
			Post post = PostsList.get(i);
			for(int j = 0; j<post.getPeoplesCount(); j++){
				if(post.getPeople(j).equals(p)){
					return post.getSalary();
				}
			}
		}
		return 0;
	}
	
	public int getPeoplesCount(){
		int z = 0;
		for(Post p: PostsList)
			z += p.getPeoplesCount();
//		for(int i = 0; i<PostsList.size(); i++){
//			z = z + PostsList.get(i).getPeoplesCount();
//		}
		return z;
	}

	public void stateChanged() {
		setChanged();
		notifyObservers();
	}
	
	public Place isNeedPeople(People people){
		Post vacancy = getVacansie();
		if(vacancy!=null)
			addPeople(vacancy, people, vacancy.getSalary());
		return this;
	}

	public void setExecuted(double worckCount) {
		this.executed = worckCount;
		stateChanged();
	}

	public double getExecuted() {
		return executed;
	}

	public int getVacansiesCount() {
		int z = 0;
		for(int i = 0; i<PostsList.size(); i++){
			if(PostsList.get(i).getPeoplesCount()==0)
				z++;
		}
		return z;
	}

	public Post getVacansie() {
		for(Post post: getPostsList())
			if(post.getPeoplesCount() == 0)
				return post;
		return null;
	}	

	public Place setPostsCount(int worckCountMax) {
		for(int i=0;i<worckCountMax;i++){
			PostsList.add(new Post(this));
		}
		return this;
	}

	public double getWorckCount() {
		return getPostsList().size();
	}

	private ArrayList<Post> getPostsList() {
		return PostsList;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public Place setName(String name) {
		Name = name;
		return this;
	}

	public String getName() {
		return Name;
	}
}